package com.cy.filters;


import com.alibaba.fastjson.JSON;
import com.cy.util.ResponseCodeEnum;
import com.cy.util.ResponseResult;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.List;


/**
 * @author Administrator
 */
@Slf4j
@Component
public class AuthFilter implements GlobalFilter, Ordered {

    public static final String Authorization = "Authorization";
    public static final String NO_AUTH_PATH = "/nacos-auth/password/login";

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        //获取response对象
        ServerHttpResponse serverHttpResponse = exchange.getResponse();
        //获取request对象
        ServerHttpRequest request = exchange.getRequest();
        //没有登录 判断是否是白名单请求
        if (isAllowRequesr(request)) {
            //如果是 放行
            return chain.filter(exchange);
        }

        //获取header中的token
        List<String> tokenlist = request.getHeaders().get("Authorization");
        if (CollectionUtils.isEmpty(tokenlist)) {
            //没有token,拦截请求
            return getVoidMono(serverHttpResponse, ResponseCodeEnum.TOKEN_MISSION);
        }
//        String token = tokenlist.get(0);
//        request.mutate().header(Authorization, "Bearer " + token, "token", token);
        return chain.filter(exchange);

    }

    /**
     * 创建response返回提示信息
     *
     * @param serverHttpResponse
     * @param responseCodeEnum
     * @return
     */
    private Mono<Void> getVoidMono(ServerHttpResponse serverHttpResponse, ResponseCodeEnum responseCodeEnum) {
        serverHttpResponse.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
        ResponseResult responseResult = ResponseResult.error(responseCodeEnum.getCode(), responseCodeEnum.getMessage());
        DataBuffer dataBuffer = serverHttpResponse.bufferFactory().wrap(JSON.toJSONString(responseResult).getBytes());
        return serverHttpResponse.writeWith(Flux.just(dataBuffer));
    }

    /**
     * 判断请求是否在白名单
     *
     * @param request
     * @return
     */
    private boolean isAllowRequesr(ServerHttpRequest request) {
        //获取当前请求的path和method
        String path = request.getPath().toString();
        String method = request.getMethodValue();
        //判断是否允许
        if (StringUtils.startsWith(NO_AUTH_PATH, path)) {
            //是许可的路径 放行
            return true;
        }
        //不是白名单请求
        return false;
    }

    @Override
    public int getOrder() {
        return -2;
    }


}
